var Outds;
var resLengthText;
var isSelected;
var outForm;
 
Ext.onReady(function(){ 

	
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
							   {name: 'boid', type: "string"},
                			   {name: 'outOrder', type: "string"},
							   {name: 'ocid', type: "string"},
                	           {name: 'ocName', type: "string"},
                	           {name: 'poleNum', type: "string"},
							   {name: 'propertyRight', type: "string"},
							   {name: 'longleath1', type: "string"},
                	           {name: 'longleath2', type: "string"},
							   {name: 'lng', type: "string"},
							   {name: 'lat', type: "string"},
							   {name: 'distance', type: "string"},
							   {name: 'isNode', type: "string"},
							   {name: 'isJoint', type: "string"},
							   {name: 'isRes', type: "string"},
							   {name: 'resLength', type: "string"},
							   {name: 'granularity', type: "string"},
							   {name: 'updateTime', type: "string"},
							   {name: 'ocLayType', type: "string"},
							   {name: 'routeInfo', type: "string"},
							   {name: 'lightningprotection', type: "string"},
							   {name: 'wiredrawing', type: "string"},
							   {name: 'strutnum', type: "string"},
							   {name: 'zone', type: "string"}
                	     ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,	            
				{header: "序号", width: 40, sortable: true, dataIndex: 'outOrder',align:'left'},
	            //{header: "中继段名称", width: 100, sortable: true, dataIndex: 'ocName',align:'left'},
	            {header: "杆号", width: 100, sortable: true, dataIndex: 'poleNum',align:'left'},
				{header: "经度", width: 70, sortable: true, dataIndex: 'lng',align:'left'},
				{header: "纬度", width: 70, sortable: true, dataIndex: 'lat',align:'left'},
	            {header: "与前一节点长度", width: 100, sortable: true, dataIndex: 'distance',align:'left'}, 
				{header: "接头", width: 50, sortable: true,renderer: formatIsNode, dataIndex: 'isJoint',align:'left'}, 	
				{header: "预留长度", width: 100, sortable: true, dataIndex: 'resLength',align:'left'},
				{header: "本点累加长度", width: 100, sortable: true, dataIndex: 'granularity',align:'left'},
				{header: "删除", width: 50, dataIndex: 'boid',renderer: showDelete}					
			]
		);
	function formatIsNode(value)
	{	
		if(value == '0')
			return "<span style='color:red;font-weight:bold;'>否</span>";
		else
			return "<span style='color:green;font-weight:bold;'>是</span>";
	}
	
	function showDelete(value, cellmeta, record, rowIndex, columnIndex, store)
	{
		var ocid = record.get("ocid");
		return "<input type='button' value='删除' class='x-btn button' onclick='doDeleteSectionOutInfo(\"" + value  +   "\",\""+ocid+"\")'>";  
	}

	Outds = new Ext.data.Store({
        proxy: new Ext.data.DWRProxy(JFiberWeb.getSectionOutInfoList, true),	
        reader: new Ext.data.ListRangeReader({
            totalProperty: 'totalSize',
            id: 'id'
        }, recordType),
        sortInfo:{field:'id', direction:'ASC'}// 排序信息
    });
 
	
	var sectionGrid = new Ext.grid.GridPanel({
	    	region : 'center',	    	
	        ds: Outds,
	        cm: cm,
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) { 
							isSelected = true;
                            outForm.getForm().loadRecord(rec);							 
							if(rec.get("isRes") == 1)
								resLengthText.enable();
							else
								resLengthText.disable();
								
							provZone = 	rec.get("zone");
							firstPropertyRight = rec.get("propertyRight");		
                        }
                    }
                }),
			//title: '(<%=strName%>)', 
			height: 400, 	
			//height: mainTabPanelDIV.getComputedHeight(),
			minSize: 200,
	        frame:true,
	        collapsible:false, 
	        enableDragDrop:false,   
			autoExpandColumn:2, 
			tbar: [
				'查询: ', ' ',
				new Ext.ux.form.SearchField({
					store:Outds,
					width:320
				})],
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: Outds,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })					
				
	    });
	//Outds.load({params:{id:'<%=strId%>',start:0,limit:17}});		
		
	var isResData = [['0', '否'],['1', '是']];
        
	var isResStore = new Ext.data.SimpleStore({
		    fields: ['id', 'text'],
		    data : isResData
		});
	
	var isNodeCombox = new Ext.form.ComboBox({	   
	        fieldLabel: '是否为节点',
	        name: 'isNode',	
	       	width:135,
	        valueField :"id",
	        displayField: "text",
	        mode: 'local',
	        selectOnFocus:true,                    
	        forceSelection: true,
	        hiddenName:'isNode',
	        triggerAction: 'all',
	        editable:false, 
	        allowBlank:false,
	        blankText :'请选是否为节点',
	        store: isResStore,
	        value:0
		});	
	var isResCombox = new Ext.form.ComboBox({	   
	        fieldLabel: '是否预留',
	        name: 'isRes',	
	       	width:50,
	        valueField :"id",
	        displayField: "text",
	        mode: 'local',
	        selectOnFocus:true,                    
	        forceSelection: true,
	        hiddenName:'isRes',
	        triggerAction: 'all',
	        editable:false, 
	        allowBlank:false,
	        blankText :'请选是否预留',
	        store: isResStore,
	        value:0
		});
	var isJointCombox = new Ext.form.ComboBox({	   
	        fieldLabel: '是否是接头',
	        name: 'isJoint',	
	       	width:135,
	        valueField :"id",
	        displayField: "text",
	        mode: 'local',
	        selectOnFocus:true,                    
	        forceSelection: true,
	        hiddenName:'isJoint',
	        triggerAction: 'all',
	        editable:false, 
	        allowBlank:false,
	        blankText :'请选是否是接头',
	        store: isResStore,
	        value:0
		});	
		
	isResCombox.addListener("select",function(){		 
			if(isResCombox.value == 1)
				resLengthText.enable();
			else
				resLengthText.disable();
		});
	isJointCombox.addListener("select",function(){		 
			if(isJointCombox.value == 1)
				ocLayTypeCombox.enable();
			else
				ocLayTypeCombox.disable();
		});	
		
	resLengthText = new Ext.form.NumberField({ 
				xtype:'numberfield',
				allowDecimals: true, 
				fieldLabel : '预留长度', 
				value:0,
				width:60,
				name : 'resLength'
			}); 
	resLengthText.disable();
 
 
	var ocLayTypeData = [['0', '电杆'],['1', '管道'],['2', '直埋']];
	var ocLayTypeStore = new Ext.data.SimpleStore({
		    fields: ['id', 'text'],
		    data : ocLayTypeData
		});
	
	var ocLayTypeCombox = new Ext.form.ComboBox({	   
	        fieldLabel: '敷设方式',
	        name: 'ocLayType',	
	       	width:135,
	        valueField :"id",
	        displayField: "text",
	        mode: 'local',
	        selectOnFocus:true,                    
	        forceSelection: true,
	        hiddenName:'ocLayType',
	        triggerAction: 'all',
	        editable:false, 
	        store: ocLayTypeStore,
	        value:0
		}); 
	ocLayTypeCombox.disable();	
	
	
	var lightningprotectionData = [['0', '否'],['1', '是']];
	var lightningprotectionStore = new Ext.data.SimpleStore({
		    fields: ['id', 'text'],
		    data : lightningprotectionData
		});
	
	var lightningprotectionCombox = new Ext.form.ComboBox({	   
	        fieldLabel: '避雷装置',
	        name: 'lightningprotection',	
	       	width:135,
	        valueField :"id",
	        displayField: "text",
	        mode: 'local',
	        selectOnFocus:true,                    
	        forceSelection: true,
	        hiddenName:'lightningprotection',
	        triggerAction: 'all',
	        editable:false, 
	        store: lightningprotectionStore,
	        value:0
		}); 
	ocLayTypeCombox.disable();	
	
	var distcodeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.scdqxslist,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 
	var distcodeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '所属地区',
		name: 'zone',	
		width:135,
		valueField :"value",
	    displayField: "text",
	    mode: 'remote',
		selectOnFocus:true,                    
		forceSelection: true,
		blankText:'请选所属地区',
		emptyText:'选择所属地区',
		hiddenName:'zone',
		triggerAction: 'all',
		allowBlank:false,
		editable:true, 
		store: distcodeStore
	});
	distcodeStore.load({});
	
		
	outForm = new Ext.form.FormPanel({
		region     : 'east', 
		labelWidth: 95,
        frame:true,
        title: '总体信息', 
        width: 320,
		//height:400,
		height   : 350,
		//autoHeight:true,
		autoScroll:true,
		//overflow:auto, 
        defaults: {width: 180},
        defaultType: 'textfield',
		items: [
				{
					xtype : 'hidden',
					name: 'boid'
				},
				{
					xtype : 'hidden',
					name: 'ocid'
				},{
					fieldLabel: '中继段名称',
					name: 'ocName', 
					readOnly:true	
				},{
					fieldLabel: '杆号',
					name: 'poleNum',
					allowBlank:false,
					blankText :'请输入杆号'	
				},{
					fieldLabel: '产权',
					name: 'propertyRight',
					allowBlank:false,
					blankText :'请输入产权'	
				},
				{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: '本点皮长',
					name: 'longleath1'
				},{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: '本点皮长',
					name: 'longleath2'
				},{
					xtype: 'panel',
					layout: 'column',
					width: 280,
					items:[
						{
								columnWidth:.60,
								layout: 'form',  
								labelAlign:'left',	
								labelWidth:95,	 
								items: [
									{
										xtype:'numberfield',
										allowDecimals: true, 
										decimalPrecision:5,
										fieldLabel: '经度',
										name: 'lng',
										maxLength:10, 
										width: 50,
										allowBlank:false,
										blankText :'请输入经度'	
									}]
						},{
								columnWidth:.40,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:35,	 
								items: [
									{
										xtype:'numberfield',
										allowDecimals: true, 
										decimalPrecision:5,
										maxLength:10,
										width: 50,
										fieldLabel: '维度',
										name: 'lat',
										allowBlank:false,
										blankText :'请输入维度'	
									}]
						}]
				},{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: '与前一节点长度',
					name: 'distance',
					allowBlank:false,
					blankText :'请输入与前一节点长度'	
				},
				isNodeCombox,
				{
					xtype: 'panel',
					layout: 'column',
					width: 280,
					items:[
						{
								columnWidth:.55,
								layout: 'form',  
								labelAlign:'left',	
								labelWidth:95,	 
								items: [isResCombox]
						},{
								columnWidth:.45,
								layout: 'form',  
								labelAlign:'right',	
								labelWidth:55,	 
								items: [ resLengthText]
						}]
				},				
				isJointCombox,
				ocLayTypeCombox,
				{
					fieldLabel: '路由信息',
					name: 'routeInfo'
				},
				lightningprotectionCombox,
				{
					xtype:'numberfield',
					allowDecimals: true, 
					decimalPrecision:2,
					fieldLabel: '拉线条数',
					name: 'wiredrawing',
					maxLength:10,
					value:0		
					//allowBlank:false,
					//blankText :'请输入经度'	
				},
				{
					xtype:'numberfield',
					allowDecimals: true, 
					decimalPrecision:2,
					fieldLabel: '撑杆数',
					name: 'strutnum',
					maxLength:10, 
					value:0						
					//blankText :'请输入经度'	
				},
				
				distcodeCombo
				
        ],
        buttons: [
				{
					text: '后退',
					handler:function()
					{
						history.go(-1);
					}
				},{
					text: '保存',
					handler:saveSectionInfo
				},{
					text: '新建/取消',
					handler:function()
					{
						isSelected = false ;
						outForm.getForm().reset();
						resetOutForm(outForm);
						resLengthText.disable();
					}
				}]
	})
	function saveSectionInfo()
	{
		var orderNum = 1;	 
		var record = sectionGrid.getSelectionModel().getSelected();// 返回值为 Record 类型
		if(record)
		{
			orderNum = 	parseInt(record.get("outOrder")) + 1;	
		}else
			orderNum = sectionGrid.getStore().getTotalCount() + 1;	
		//alert("sectionGrid.getStore().getTotalCount() = "+sectionGrid.getStore().getTotalCount() +"\norderNum = "+orderNum);
		//return;
		if (!outForm.getForm().isValid())
			return;	
		var form = outForm.getForm().getValues(false);
		SectionOutInfoBO = {};
		SectionOutInfoBO.id = form.boid;
		SectionOutInfoBO.outOrder = orderNum;
		SectionOutInfoBO.ocid = ocid;
		SectionOutInfoBO.ocName = form.ocName;
		SectionOutInfoBO.poleNum = form.poleNum;
		SectionOutInfoBO.propertyRight = form.propertyRight;
		SectionOutInfoBO.longleath1 = form.longleath1;
		SectionOutInfoBO.longleath2 = form.longleath2;
		SectionOutInfoBO.lng = form.lng;
		SectionOutInfoBO.lat = form.lat;
		SectionOutInfoBO.distance = form.distance;
		SectionOutInfoBO.isNode = form.isNode;
		SectionOutInfoBO.isRes = form.isRes;
		SectionOutInfoBO.resLength = resLengthText.getValue(); 
		SectionOutInfoBO.ocLayType = ocLayTypeCombox.getValue(); 
		SectionOutInfoBO.routeInfo = form.routeInfo; 
		SectionOutInfoBO.isJoint = form.isJoint;
		firstPropertyRight = form.propertyRight;	
		
		SectionOutInfoBO.lightningprotection = form.lightningprotection;
		SectionOutInfoBO.wiredrawing = form.wiredrawing;	
		SectionOutInfoBO.strutnum = form.strutnum;	
		SectionOutInfoBO.zone = form.zone;	
		provZone = form.zone;
		JFiberWeb.createSectionOutInfo(SectionOutInfoBO,function(data)
		{
			isSelected = false;
			//outForm.getForm().reset();
			resLengthText.disable();
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				//Outds.load({params:{id:'<%=strId%>',start:0,limit:17}});
				outForm.getForm().reset();
				loadDate(Outds,outForm);	
				ocLayTypeCombox.setValue(SectionOutInfoBO.ocLayType);
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}	
		})
	}

	var displayPanel = new Ext.Panel({
		width    : mainTabPanelDIV.getComputedWidth(),
		height   : mainTabPanelDIV.getComputedHeight()-20,
		//height   : 378,
		layout   : 'border',
		//autoScroll:true,
		renderTo : 'outInfoTab',
		items    : [
			sectionGrid,
			outForm
		]
	});

	//loadDate(Outds,outForm);	
})	

function doDeleteSectionOutInfo(id,ocid)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该户外信息?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	
			 isSelected = false;
			 outForm.getForm().reset();
			 resLengthText.disable();
			 JFiberWeb.deleteSectionOutInfo(id,ocid, function(data) {
			 if(data) {							    	     		
				 loadDate(Outds,outForm);
			 }else 
			 {
				Ext.Msg.alert("删除失败","数据删除失败!");
			 }
			 });
			
		 }
		}
	);
}	


